package base;

import java.util.*;
import com.swabunga.spell.event.SpellChecker;

public class Word
{
	public static char[] letters={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
	
	private char[] word;
	private char[] target;
	private int level;
	private int heuristic;
	private Word parent;
	
	public Word(char[] word,char[] target)
	{
		this(word,target,0,null);
	}
	
	public Word(char[] word,char[] target,int level,Word parent)
	{
		this.word=word;
		this.target=target;
		this.level=level;
		this.parent=parent;
		heuristic=generateHeuristic();
	}
	
	
	
	private int generateHeuristic()
	{
		//lower is better
		int score=0;
		for(int i=0;i<Math.min(word.length,target.length);i++)
			if(word[i]==target[i])
				score+=1;
		
		return level-2*score;
	}
	
	
	public int getHeuristic()
	{
		return heuristic;
	}
	public int getLevel()
	{
		return level;
	}
	public char[] getWord()
	{
		return word;
	}
	public Word getParent()
	{
		return parent;
	}
	
	public ArrayList<Word> mutate(SpellChecker checker)
	{
		ArrayList<Word> wordList=new ArrayList<Word>();
		
		
		for(int i=0;i<word.length;i++)
			for(int j=0;j<Word.letters.length;j++)
			{
				char[] newWord=Arrays.copyOf(word,word.length);
				
				newWord[i]=Word.letters[j];
				
				if(newWord[i]!=word[i])	
					if(checker.isCorrect(new String(newWord)) || Arrays.equals(newWord,target))
					{
						wordList.add(new Word(newWord,target,level+1,this));
					}
					
			}
		
		return wordList;
	}	
}
